﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SKG.DAL
{
    using Entities;
    using SKG.Extend;
    using System.Data;

    public class Ktx_LophocDAL : BaseDAL
    {
        #region Implement
        /// <summary>
        /// Count number of records
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return _db.Lophocs.Count();
        }

        /// <summary>
        /// Return data by foreign key
        /// </summary>
        /// <param name="fKey">Foreign key on this table</param>
        /// <returns></returns>
        public DataTable Select(Guid fKey)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Select object of entity by secondary primary key (when need to)
        /// </summary>
        /// <param name="code">Primary key (when need to)</param>
        /// <returns></returns>
        public object Select(string code)
        {
            try
            {
                return _db.Lophocs.SingleOrDefault(s => s.Malop == code);
            }
            catch { return null; }
        }

        /// <summary>
        /// Return data
        /// </summary>
        /// <param name="obj">Object of entity</param>
        /// <param name="skip">Skip number of records</param>
        /// <param name="take">Take number of records</param>
        /// <returns></returns>
        public DataTable Select(object obj = null, int skip = 0, int take = 0)
        {
            try
            {
                var res = from s in _db.Lophocs 
                          orderby s.Tenlop
                          select new
                          {
                              s.Id,
                              s.Malop,
                              s.Tenlop,
                              s.Nienkhoa,
                              s.KhoaId,
                              s.Khoa.Tenkhoa
                          };
                // return  Data.ToDataTable(res);
                return res.ToDataTable();

            }
            catch { return _tb; }
        }

        /// <summary>
        /// Insert data
        /// </summary>
        /// <param name="obj">Object of entity</param>
        /// <returns></returns>
        public object Insert(object obj)
        {
            try
            {
                var o = (Ktx_Lophoc)obj;
                if (Select(o.Malop) != null) return null; // already exists
                o.Id = Guid.NewGuid();
                var oki = _db.Lophocs.Add(o);

                _db.SaveChanges();
                return oki;
            }
            catch { return null; }
        }

        /// <summary>
        /// Update data
        /// </summary>
        /// <param name="obj">Object of entity</param>
        /// <returns></returns>
        public object Update(object obj)
        {
            try
            {
                var o = (Ktx_Lophoc)obj;
                var res = _db.Lophocs.SingleOrDefault(s => s.Malop == o.Malop);

                res.Tenlop = o.Tenlop;
                res.Nienkhoa = o.Nienkhoa;
                res.KhoaId = o.KhoaId;

                return _db.SaveChanges();
            }
            catch { return null; }
        }

        /// <summary>
        /// Delete data
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns></returns>
        public object Delete(Guid code)
        {
            try
            {
                if (code != new Guid())
                {
                    var res = _db.Lophocs.SingleOrDefault(uuu => uuu.Id == code);
                    _db.Lophocs.Remove(res);
                }
                else
                {
                    var tmp = _db.Lophocs.ToList();
                    tmp.ForEach(s => _db.Lophocs.Remove(s));
                }
                return _db.SaveChanges();
            }
            catch { return null; }
        }
        #endregion
    }
}
